package com.example.lanouhn.firstapp.activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.model.AddressManagement;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.List;

/**
 * 地址管理数据适配器
 */
public class AddressManagementAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<AddressManagement> list;

    private DbManager dbManager;

    public AddressManagementAdapter(List<AddressManagement> list, Context context) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        holder = new VHolder(
                LayoutInflater.from(context).inflate(R.layout.item_address_management, null));

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        VHolder vHolder = (VHolder) holder;

        AddressManagement address = list.get(position);

        if(address.getName() != null && !address.getName().equals("")){
            vHolder.name.setText(address.getName());

        }
       if(address.getPhone() != null && !address.getPhone().equals("")){
           vHolder.phone.setText(address.getPhone());
       }

        if(address.getProvince() != null && !address.getProvince().equals("")){
            vHolder.province.setText(address.getProvince());
        }
        if(address.getCountry() != null && !address.getCountry().equals("")){
            vHolder.country.setText(address.getCountry());
        }
        if(address.getStreet() != null && !address.getStreet().equals("")){
            vHolder.street.setText(address.getStreet());
        }
        vHolder.address_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                try {
                    dbManager.deleteById(AddressManagement.class,list.get(position));
                } catch (DbException e) {
                    e.printStackTrace();
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //创建内部类VHolder
    class VHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private TextView phone;
        private TextView province;
        private TextView country;
        private TextView street;
        private TextView address_delete;

        //绑定控件
        public VHolder(View itemView) {
            super(itemView);

            name = (TextView) itemView.findViewById(R.id.address_name);
            phone = (TextView) itemView.findViewById(R.id.address_phone);
            province = (TextView) itemView.findViewById(R.id.address_province);
            country = (TextView) itemView.findViewById(R.id.address_country);
            street = (TextView) itemView.findViewById(R.id.address_street);
            address_delete = (TextView) itemView.findViewById(R.id.address_delete);

        }
    }
}
